package com.qianfeng.wuyang.ximalaya.activity;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianfeng.wuyang.ximalaya.R;
import com.squareup.picasso.Picasso;

import java.util.Collection;
import java.util.List;

/**
 * Created by ShenHao on 2016/12/2.
 */

public class Adapter_Fragment_Classify extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private static final int HEADER = 0;
    public static final int CONTENT = 1;
    public static final int BOTTOM = 2;

    private int mheaderCount = 1;
    private int mbottomCount = 1;

    private List<Entity_Classify> list_classify;
    private Context context;

    public Adapter_Fragment_Classify(List<Entity_Classify> list_classify, Context context) {
        this.list_classify = list_classify;
        this.context = context;
    }

    public int getContentItemCount() {
        return (list_classify.size() + 3) / 4;
    }

    public boolean isHeader(int position) {
        return mheaderCount != 0 && position < mheaderCount;
    }

    public boolean isBottom(int position) {
        return mbottomCount != 0 && position >= mheaderCount + getContentItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeader(position)) {
            return HEADER;
        } else if (isBottom(position)) {
            return BOTTOM;
        } else {
            return CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i("1234","onCreateViewHolder");
        if (viewType == HEADER) {
            return new HeaderViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_fragment_classify_headview, parent, false));
        } else if (viewType == BOTTOM) {
            return new BottomViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_fragment_classify_root, parent, false));
        } else {
            return new Fragment_Classify_ViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_fragment_inlude_1, parent, false));
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Log.i("1234","onBindViewHolder");
        if (holder instanceof HeaderViewHolder&&position==0) {
            HeaderViewHolder haedViewHolder= (HeaderViewHolder) holder;
            Log.i("1234",list_classify.size()+"");
            Picasso.with(context).load(list_classify.get(position).getCoverPath()).into( haedViewHolder.image);
        } else if (holder instanceof Fragment_Classify_ViewHolder) {

            Fragment_Classify_ViewHolder contentHolder = (Fragment_Classify_ViewHolder) holder;
           {//position!=0
                Picasso.with(context).load(list_classify.get(2 * position - 1).getCoverPath()).into(contentHolder.image_frament);
                contentHolder.textView.setText(list_classify.get(2 * position - 1).getTitle());

                Picasso.with(context).load(list_classify.get(2 * position).getCoverPath()).into(contentHolder.image_fragment_2);
                contentHolder.textView_2.setText(list_classify.get(2 * position).getTitle());

                //添加空行
                LinearLayout view_1 = (LinearLayout) contentHolder.textView.getParent();
                LinearLayout view_2 = (LinearLayout) contentHolder.image_fragment_2.getParent();
                if ((position) % 3 == 0) {

                    LinearLayout.LayoutParams params_1 = (LinearLayout.LayoutParams) view_1.getLayoutParams();
                    params_1.setMargins(0, 0, 1, 20);

                    view_1.setLayoutParams(params_1);

                    LinearLayout.LayoutParams params_2 = (LinearLayout.LayoutParams) view_2.getLayoutParams();
                    params_2.setMargins(1, 0, 0, 20);
                    view_2.setLayoutParams(params_2);
                }
            }
        } else {

        }
    }

    @Override
    public int getItemCount() {
        Log.i("1234",(list_classify.size() + 3) / 4+"");
        return 2 + (list_classify.size() + 3) / 4;
    }

    public void addAll(Collection<? extends Entity_Classify> collection) {
        list_classify.addAll(collection);
        notifyDataSetChanged();
    }

    public class Fragment_Classify_ViewHolder extends RecyclerView.ViewHolder {

        ImageView image_frament, image_fragment_2, imageView;
        TextView textView, textView_2;

        public Fragment_Classify_ViewHolder(View itemView) {
            super(itemView);
            image_frament = (ImageView) itemView.findViewById(R.id.include_classify_image);
            textView = (TextView) itemView.findViewById(R.id.include_classify_text);


            image_fragment_2 = (ImageView) itemView.findViewById(R.id.include_classify_image_2);
            textView_2 = (TextView) itemView.findViewById(R.id.include_classify_text_2);


            //imageView= (ImageView) itemView.findViewById(R.id.image_fragement_classify);
        }
    }

    public static class HeaderViewHolder extends RecyclerView.ViewHolder {
        private ImageView image;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.head_image);
        }
    }

    public static class BottomViewHolder extends RecyclerView.ViewHolder {

        public BottomViewHolder(View itemView) {
            super(itemView);
        }
    }

}
